package cm.tl.pms.sys.controller;

import cm.tl.pms.common.entity.ResultMsg;
import cm.tl.pms.sys.entity.SysPower;
import cm.tl.pms.sys.pojo.MenuVo;
import cm.tl.pms.sys.service.ISysMenuService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统-权限菜单管理
 *
 * @author laker
 * @since 2021-08-04
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {
    @Autowired
    ISysMenuService sysMenuService;

    /**
     * 系统菜单表分页查询
     *
     * @param current
     * @param size
     * @return
     */
    @GetMapping
    public ResultMsg<Page<SysPower>> pageAll(@RequestParam(required = false, defaultValue = "1") long current,
                                             @RequestParam(required = false, defaultValue = "10") long size) {
        Page<SysPower> roadPage = new Page<>(current, size);
        LambdaQueryWrapper<SysPower> queryWrapper = new LambdaQueryWrapper<>();
        Page<SysPower> pageList = sysMenuService.page(roadPage, queryWrapper);
        return ResultMsg.successfulResult(pageList);
    }


    /**
     * 系统菜单表分页查询
     *
     * @return
     */
    @GetMapping("/list")
    public ResultMsg<List<SysPower>> list() {
        List<SysPower> list = sysMenuService.list(Wrappers.<SysPower>lambdaQuery().orderByAsc(SysPower::getSort));
        return ResultMsg.successfulResult(list);
    }

    /**
     * 新增或者更新系统菜单表
     *
     * @param param
     * @return
     */
    @PostMapping
    @SaCheckPermission("menu.update")
    public ResultMsg<Boolean> saveOrUpdate(@RequestBody SysPower param) {
        return ResultMsg.successfulResult(sysMenuService.saveOrUpdate(param));
    }

    /**
     * 根据id查询系统菜单表
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultMsg<SysPower> get(@PathVariable Long id) {
        return ResultMsg.successfulResult(sysMenuService.getById(id));
    }


    /**
     * 菜单树
     *
     * @return
     */
    @GetMapping("/tree")
    public List<MenuVo> tree() {
        return sysMenuService.menu();
    }


    /**
     * 菜单列表树
     *
     * @return
     */
    @GetMapping("/selectTree")
    public ResultMsg<List<MenuVo>> selectTree() {
        List<MenuVo> menuVos = sysMenuService.menu();
        return ResultMsg.successfulResult(menuVos);
    }

    /**
     * 根据id删除系统菜单表
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("menu.delete")
    public ResultMsg<Boolean> delete(@PathVariable Long id) {
        return ResultMsg.successfulResult(sysMenuService.removeById(id));
    }
}
